﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;

public partial class frmsignup : System.Web.UI.Page
{
    SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["Roominfodb"].ConnectionString);
    public string query1;
    public void refresh()
    {

        txtaddress.Text = "";
        txtemailid.Text = "";
        txtmobile.Text = "";
        txtpassword.Text = "";
        txtUsername.Text = "";
        lblError.Text = string.Empty;
    }

    public void ExecNonQuery(SqlConnection myconn, string q1)
    {
        try
        {
            if (myconn.State == ConnectionState.Open)
            {
                myconn.Close();
                myconn.Open();
            }
            else
            {
                myconn.Open();
            }
            SqlCommand mycmd = new SqlCommand(q1, myconn);
            mycmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        { lblError.Text = Convert.ToString(ex); }
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        // calender.Visible = false;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (!Page.IsPostBack)
            {
                //myconn.ConnectionString="Data Source=roominfo.db.7599184.hostedresource.com; Initial Catalog=roominfo; User ID=roominfo; Password='Elango123456';" ;
                myconn.Open();
                //Page.Title = "Connection Opened";
                lblError.Text = "";
            }
        }
        catch
        {

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        refresh();
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        try
        {
            if (myconn.State == ConnectionState.Open)
            {
                myconn.Close();
                myconn.Open();
            }
            else
            {
                myconn.Open();
            }
            SqlCommand mycmd = new SqlCommand("select * from userinfo where userid='@1'", myconn);
            mycmd.Parameters.AddWithValue("@1", txtUsername.Text);
            SqlDataReader myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
            if (myReader.HasRows)
            {
                string err = "Username Already Exist, Please Try New";
                //  commonlabel.text=""; 
                string script = "alert('" + err + "');\n";
                Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", script, true);
                txtUsername.Focus();
            }
            else
            {
                if (myconn.State == ConnectionState.Open)
                {
                    myconn.Close();
                    myconn.Open();
                }
                else
                {
                    myconn.Open();
                }
                lblError.Text = "";
                query1 = "insert into userinfo (userid,username,password,addr,dob,emailid,mobile,status) values('" + txtUsername.Text + "','" + txtUsername.Text + "','" + txtpassword.Text + "','" + txtaddress.Text + "','" + DateTime.Now + "','" + txtemailid.Text + "','" + txtmobile.Text + "','0')";
                //SqlCommand mycmd1 = new SqlCommand(query1, myconn);
                //mycmd1.Parameters.AddWithValue("@1", txtUsername.Text);
                //mycmd1.Parameters.AddWithValue("@2", txtpassword.Text);
                //mycmd1.Parameters.AddWithValue("@3",txtaddress.Text);
                //mycmd1.Parameters.AddWithValue("@4",txtemailid.Text);
                //mycmd1.Parameters.AddWithValue("@5",Convert.ToInt64(txtmobile.Text));
                //mycmd1.ExecuteNonQuery();
                ExecNonQuery(myconn, query1);
                lblError.Text = " Username:'" + txtUsername.Text + "' Created Successfully.";
                string err1 = " Username:'" + txtUsername.Text + "' Created Successfully.";
                string script1 = "alert('" + err1 + "')";
                Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", script1, true);

                //                Imports System.Net
                //Imports System.Net.Mail
                //Public Class frmMail
                //    'Dim obj As SmtpMail
                //    'Variable to store the attachments 
                //    ' Dim Attachment As System.Web.Mail.MailAttachment
                //    'Variable to  follcreate the message to send
                //    'Dim Mailmsg As New MailMessage()
                //    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                //        Dim client As New SmtpClient()

                SmtpClient Client = new SmtpClient();
                MailAddress sentTo = new MailAddress("elangovan@beklans.com");
                MailAddress from = new MailAddress("elangovan@beklans.com");
                MailMessage message = new MailMessage(from, sentTo);
                message.IsBodyHtml = true;
                message.Subject = "New Account Activation";
                message.Body = "Please activate the following account:" + txtUsername.Text;
                System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("elangovan@beklans.com", "123456"); ;
                Client.Host = "smtpout.asia.secureserver.net";
                Client.UseDefaultCredentials = false;
                Client.Credentials = basicAuthenticationInfo;
                try
                {
                    Client.Send(message);
                    lblError.Text = "Account Forwaded to Administrator,Please wait till Admin activates the account";
                    Session["msg"] = "Account Forwaded to Administrator,Please wait till Admin activates the account";
                    // Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Account Forwaded to Administrator,Please wait till Admin activates the account');\n", true);
                    // Response.Redirect("~/frmlogin.aspx");
                }
                catch (HttpException ex) { ltMsg.Text = ex + "Please Inform admin to activate ur account, ADMIN Mail ID: mailto:elangovan@beklans.com"; }
                Response.Redirect("~/frmlogin.aspx?msg=" + lblError.Text);
                refresh();
            }
        }
        catch (SqlException ex)
        {
            lblError.Text = Convert.ToString(ex);
        }

    }

}
